package com.smarth.zhizulian.fragment;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.R;
import com.smarth.zhizulian.adapter.GetMoneyAdapter;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.bean.profit.ProfitCashApply;
import com.smarth.zhizulian.bean.Page;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.bad.BadListObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpListDataModel;
import com.smarth.zhizulian.utils.listhelper.impl.model.ListDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.IDataAdapter;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by 18865 on 2017/12/25.
 */

public class GetMoneyFragment extends EventFragment {
    @BindView(R.id.lv_listview)
    ListView lvListview;
    @BindView(R.id.rv_refresh)
    SmartRefreshLayout rvRefresh;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private List<ProfitCashApply> mData = new ArrayList<>();
    private GetMoneyAdapter mAdapter;
    private RefreshHelper<List<ProfitCashApply>> refreshHelper;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.fragment_moneydetail);
        initList();
    }

    @Override
    protected void onLogin(EventBean eb) {
        refreshHelper.refresh();
    }

    private void initList() {

        refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, rvRefresh);
        refreshHelper.setDataModel(new HttpListDataModel<List<ProfitCashApply>>(mContext) {
            @Override
            public Observable<? extends Page> getObservable(ResponseSender<List<ProfitCashApply>> responseSender, int page) {
                return KumaHttp.getService().profitCashApplyPage(page).compose(RxCompos.<Page<ProfitCashApply>>getBELoginCompos());
            }
        });
        refreshHelper.setDataAdapter(new MyGetMoneyAdapter(mContext, new ArrayList<>()));
        refreshHelper.refresh();
    }

    @Subscriber(tag = BaseConfig.EventTag.ON_GETMONEY)
    private void onGetMoney(EventBean eb) {
        refreshHelper.refresh();
    }

    private class MyGetMoneyAdapter extends GetMoneyAdapter implements IDataAdapter<List<ProfitCashApply>> {

        public MyGetMoneyAdapter(Context context, List<?> datas) {
            super(context, datas);
        }

        @Override
        public void onGetData(List<ProfitCashApply> profitProfits, boolean isRefresh) {
            if (isRefresh) {
                mDatas.clear();
            }
            mDatas.addAll(profitProfits);
            notifyDataSetChanged();
        }

        @Override
        public boolean isDataEmpty() {
            return mDatas == null || mDatas.size() == 0;
        }

        @Override
        public List<ProfitCashApply> getMyData() {
            return mDatas;
        }
    }

}
